﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace _1stVATCWin
{
    public partial class ListNhaCungUng : Form
    {
        public ListNhaCungUng()
        {
            InitializeComponent();
        }

        private void ListNhaCungUng_Load(object sender, EventArgs e)
        {
            loadlistNCU();
        }
        private void loadlistNCU()
        {
            Config file = new Config(Application.StartupPath + "\\config.ini");
            string connectionString = file.ReadValue("server", "conn");
            SqlConnection conn = new SqlConnection(connectionString);
            string sql = " select * from NHACUNGCAP";
            SqlCommand cmd = new SqlCommand(sql, conn);

            SqlDataAdapter adapter = new SqlDataAdapter(cmd);
            conn.Open();
            DataSet ds = new DataSet();
            adapter.Fill(ds, "Table NHACUNGCAP");
            DataTable dt = ds.Tables["Table NHACUNGCAP"];

            //dataGridView1.AutoGenerateColumns = false;
            dataGridView1.DataSource = ds.Tables["Table NHACUNGCAP"].DefaultView;
        }
        private void loadlistloai()
        {
            int i = dataGridView1.SelectedRows[0].Index;
            string id = dataGridView1.Rows[i].Cells["IDNCC"].Value.ToString();

            Config file = new Config(Application.StartupPath + "\\config.ini");
            string connectionString = file.ReadValue("server", "conn");
            SqlConnection conn = new SqlConnection(connectionString);
            string sql = "select * from  NCC_CT where IDNCC=@IDNCC";
            SqlCommand cmd = new SqlCommand(sql, conn);

            cmd.Parameters.AddWithValue("@IDNCC", id);
            SqlDataAdapter adapter = new SqlDataAdapter(cmd);
            conn.Open();
            DataSet ds = new DataSet();
            adapter.Fill(ds, "Table NCC_CT");
            DataTable dt = ds.Tables["Table NCC_CT"];

            dataGridView2.AutoGenerateColumns = false;
            dataGridView2.DataSource = ds.Tables["Table NCC_CT"].DefaultView;
        }

        private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            autoload();
            loadlistloai();
        }

        private void autoload()
        {
            int i = dataGridView1.SelectedRows[0].Index;
            string IDNCC = dataGridView1.Rows[i].Cells["IDNCC"].Value.ToString();

            Config file = new Config(Application.StartupPath + "\\config.ini");
            string connectionString = file.ReadValue("server", "conn");
            SqlConnection conn = new SqlConnection(connectionString);
            string sql = "select * from NHACUNGCAP where IDNCC=@IDNCC";
            SqlCommand cmd = new SqlCommand(sql, conn);

            cmd.Parameters.AddWithValue("@IDNCC", IDNCC);
            conn.Open();
            SqlDataReader r = cmd.ExecuteReader();
            while (r.Read())
            {
                textBox1.Text = r["tenNCC"].ToString();
                textBox2.Text = r["diachiNCC"].ToString();
                textBox3.Text = r["emailNCC"].ToString();
                textBox4.Text = r["sodtNCC"].ToString();
            }
            r.Close();
            conn.Close();
        }

        private void ADD_Click(object sender, EventArgs e)
        {
            NhapNCC f = new NhapNCC();
            if (f.ShowDialog() == DialogResult.OK)
            {
                loadlistNCU();
            }

        }

        private void Update_Click(object sender, EventArgs e)
        {
            if ((MessageBox.Show("Bạn có muốn sửa dữ liệu không?", "Thông Báo", MessageBoxButtons.OKCancel)) == DialogResult.OK)
            {
                int i = dataGridView1.SelectedRows[0].Index;
                string IDNCC = dataGridView1.Rows[i].Cells["IDNCC"].Value.ToString();

                Config file = new Config(Application.StartupPath + "\\config.ini");
                string connectionString = file.ReadValue("server", "conn");
                SqlConnection conn = new SqlConnection(connectionString);
                string sql = "update NHACUNGCAP set tenNCC=@tenNCC,diachiNCC=@diachiNCC,emailNCC=@emailNCC,sodtNCC=@sodtNCC where IDNCC=@IDNCC";
                SqlCommand cmd = new SqlCommand(sql, conn);

                cmd.Parameters.AddWithValue("@IDNCC", IDNCC);
                cmd.Parameters.AddWithValue("@tenNCC", textBox1.Text);
                cmd.Parameters.AddWithValue("@diachiNCC", textBox2.Text);
                cmd.Parameters.AddWithValue("@emailNCC", textBox3.Text);
                cmd.Parameters.AddWithValue("@sodtNCC", textBox4.Text);

                conn.Open();
                cmd.ExecuteNonQuery();
                conn.Close();

                loadlistNCU();
            }

        }

        private void Delete_Click(object sender, EventArgs e)
        {
            if ((MessageBox.Show("Bạn có muốn xóa dữ liệu không?", "Thông Báo", MessageBoxButtons.OKCancel)) == DialogResult.OK)
            {
                int i = dataGridView1.SelectedRows[0].Index;
                string IDNCC = dataGridView1.Rows[i].Cells["IDNCC"].Value.ToString();
                Config file = new Config(Application.StartupPath + "\\config.ini");
                string connectionString = file.ReadValue("server", "conn");
                SqlConnection conn = new SqlConnection(connectionString);
                string sql = "delete from NHACUNGCAP where IDNCC = @IDNCC";
                SqlCommand cmd = new SqlCommand(sql, conn);
                cmd.Parameters.AddWithValue("@IDNCC", IDNCC);

                conn.Open();
                cmd.ExecuteNonQuery();
                conn.Close();

            }
            loadlistNCU();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            int i = dataGridView1.SelectedRows[0].Index;
            string id = dataGridView1.Rows[i].Cells["IDNCC"].Value.ToString();

            AddLoai f = new AddLoai(id);
            if (f.ShowDialog() == DialogResult.OK)
            {
                loadlistloai();

            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            {
                if ((MessageBox.Show("Bạn có muốn xóa dữ liệu không?", "Thông Báo", MessageBoxButtons.OKCancel)) == DialogResult.OK)
                {
                    int i = dataGridView2.SelectedRows[0].Index;
                    string IDloai = dataGridView2.Rows[i].Cells["IDloai"].Value.ToString();
                    Config file = new Config(Application.StartupPath + "\\config.ini");
                    string connectionString = file.ReadValue("server", "conn");
                    SqlConnection conn = new SqlConnection(connectionString);
                    string sql = "delete from NCC_CT where IDloai = @IDloai";
                    SqlCommand cmd = new SqlCommand(sql, conn);
                    cmd.Parameters.AddWithValue("@IDloai", IDloai);

                    conn.Open();
                    cmd.ExecuteNonQuery();
                    conn.Close();

                }
                loadlistloai();
            }



        }

        private void button3_Click(object sender, EventArgs e)
        {
            HopDong f = new HopDong();
            if (f.ShowDialog() == DialogResult.OK)
            {
                ListNhaCungUng g = new ListNhaCungUng();
                g.ShowDialog();
            }
        }
    }
}
